// var url = 'http://localhost';
var url = 'http://ycs.acocomoon.com';
layui.use(['layer'], function () {
    var layer = layui.layer;
    $(document).ajaxStart(function () {
        layer.load(1, {
            shade: [0.3, '#fff'] //0.1透明度的白色背景
        })
    });
    $(document).ajaxError(function (event, xhr, options, exc) {
        layer.open({
            title: '异常',
            content: '数据请求异常',
            area: '420px',
            btn: ['刷新', '关闭'],
            resize: false,
            yes: function () {
                if (xhr.status == 40000) {//登录超时
                    top.location.href = './login.html'
                } else {
                    location.reload();
                }
            }
        });
    });
    $(document).ajaxComplete(function () {
        layer.closeAll('loading');
    });
})

function permission() {
    var auto = $('[auth]');
    var permission = $.parseJSON(localStorage.getItem('permission'));
    for (var k = 0; k < auto.length; k++) {
        if (permission.indexOf($(auto[k]).attr('auth')) == -1) {
            $(auto[k]).remove();
        } else {
            $(auto[k]).show();
        }
    }
}

function hasPermission(auth) {
    var permission = $.parseJSON(localStorage.getItem('permission'));
    if (permission.indexOf(auth) === -1) {
        return false;
    } else {
        return true;
    }
}

function unlogin(title, r) {
    if (r.responseJSON == undefined) {
        layui.use('layer', function () {
            var layer = layui.layer;
            layer.open({
                type: 1,
                title: '登陆超时',
                area: ["40%", '30%'],
                content: `<p class='one_p'>网络请求错误!</p>`,
                btn: ['关闭'],
                skin: 'my-skin'
            })
        });
        setTimeout(function () {
            localStorage.clear();
            window.top.location = './login.html';
        }, 2000);
    } else {
        if (r.responseJSON.code == 100001) {
            layui.use('layer', function () {
                var layer = layui.layer;
                layer.open({
                    type: 1,
                    title: '登陆超时',
                    area: ["40%", '30%'],
                    content: `<p class='one_p'>${r.responseJSON.msg}</p>`,
                    btn: ['关闭'],
                    skin: 'my-skin'
                })
            });
            setTimeout(function () {
                localStorage.clear();
                window.top.location = './login.html';
            }, 2000)
        } else {
            layui.use('layer', function () {
                var layer = layui.layer;
                layer.open({
                    type: 1,
                    title: title,
                    area: ["40%", '30%'],
                    content: `<p class='one_p'>${r.responseJSON.msg}</p>`,
                    btn: ['关闭'],
                    skin: 'my-skin'
                })
            });
        }
    }
}

// 整数
function limitInput_1(o) {
    var reg = new RegExp(/^[0-9]{1}?$/);
    if (reg.test(o.value)) {
        o.value = o.value
    } else {
        o.value = Number(o.value).toFixed(0);
    }
}

//日期
function fomDate(time) {
    var date = new Date();
    var yyyy = date.getFullYear();//四位的年份
    var mm = Number(date.getMonth()) + 1;//月份
    var dd = date.getDate();//当前日
    var HH = date.getHours();//小时
    var MM = date.getMinutes();//分钟
    var SS = date.getSeconds();//秒
    if (mm.toString().length == 1) {
        mm = '0' + mm;
    }
    if (dd.toString().length == 1) {
        dd = '0' + dd;
    }
    if (MM.toString().length == 1) {
        MM = '0' + MM;
    }
    if (SS.toString().length == 1) {
        SS = '0' + SS;
    }
    if (time == 'yyyy-mm-dd') {
        return yyyy + '-' + mm + '-' + dd;
    } else if (time = 'yyyy-mm-dd HH:MM:SS') {
        return yyyy + '-' + mm + '-' + dd + ' ' + HH + ':' + MM + ':' + SS;
    }
}

/*
 * 取token
 */
function getToken() {
    return $.parseJSON(localStorage.getItem('login_msg')).token;
}

/*
 * ajax操作提示
 */
function tips(tipsName, r) {
    if (r.msg == "") {
        r.msg = '操作成功!'
    }
    layer.open({
        title: tipsName,
        content: r.msg,
        area: '420px',
        offset: '15rem',
        btn: ['关闭']
    });
}

/*
 * @name 项目下拉列表
 * @return {string}
 */
function projectSelect(isAll = 0) {
    var option = '<option value="0">-</option>';
    $.ajax({
        url: url + '/project/project/selectLists',
        type: "POST",
        async: false,
        data: {
            isAll: isAll,
            token: getToken()
        },
        success: function (r) {
            if (r.code == 100000) {
                for (var i = 0; i < r.data.length; i++) {
                    option += `
                        <option value=${r.data[i].id}>${r.data[i].name}</option>
                    `
                }
            } else {
                tips('项目下拉列表', r);
            }
        }
    });
    return option;
}

/*
 * @name 工种下拉列表
 * @return {string}
 */
function professionSelect(all) {
    var option = '';
    if (all == 1) {
        option = '<option value="">全部</option>';
    } else {
        option = '<option value="">-</option>';
    }
    $.ajax({
        url: url + '/setting/profession/lists',
        type: "POST",
        async: false,
        data: {
            token: getToken()
        },
        success: function (r) {
            if (r.code == 100000) {
                for (var i = 0; i < r.data.data.length; i++) {
                    option += `
                        <option value=${r.data.data[i].id}>${r.data.data[i].name}</option>
                    `
                }
            } else {
                tips('工种下拉列表', r);
            }
        }
    });
    return option;
}

/*
 * @name 计量单位下拉列表
 * @return {string}
 */
function unitSelect() {
    var option = '<option value="">请选择单位</option>';
    $.ajax({
        url: url + '/setting/unit/lists',
        type: "POST",
        async: false,
        data: {
            token: getToken()
        },
        success: function (r) {
            if (r.code == 100000) {
                for (var i = 0; i < r.data.length; i++) {
                    option += `
                        <option value=${r.data[i].id}>${r.data[i].name}</option>
                    `
                }
            } else {
                tips('计量单位下拉列表', r);
            }
        }
    });
    return option;
}

/**
 *
 */
function yearSelect() {
    var option = '';
    $.ajax({
        url: url + '/setting/year/lists',
        type: "POST",
        async: false,
        data: {
            token: getToken()
        },
        success: function (r) {
            if (r.code == 100000) {
                for (var i = 0; i < r.data.length; i++) {
                    option += `
                        <option value=${r.data[i].id} ${i == 0 ? 'selected' : ''}>${r.data[i].name}</option>
                    `
                }
            } else {
                tips('工种下拉列表', r);
            }
        }
    });
    return option;
}

/**
 *
 */
function supplierSelect(isNew = false) {
    var option = '<option value="">-</option>';
    $.ajax({
        url: url + '/hrm/supplier/selectLists',
        type: "POST",
        async: false,
        data: {
            token: getToken()
        },
        success: function (r) {
            if (r.code == 100000) {
                for (var i = 0; i < r.data.length; i++) {
                    option += `
                        <option value=${r.data[i].id}>${r.data[i].name}</option>
                    `
                }
            } else {
                tips('供应商下拉列表', r);
            }
        }
    });
    if (isNew) {
        option += '<option value="0">请输入新的供应商</option>';
    }
    return option;
}

/**
 *
 */
function materialSelect() {
    var option = '<option value="">-</option>';
    $.ajax({
        url: url + '/setting/material/selectLists',
        type: "POST",
        async: false,
        data: {
            token: getToken()
        },
        success: function (r) {
            if (r.code == 100000) {
                for (var i = 0; i < r.data.length; i++) {
                    option += `
                         <option value="${r.data[i].id}">${r.data[i].name}</option>
                    `
                }
            } else {
                tips('材料下拉列表', r);
            }
        }
    });
    return option;
}


function specSelect(id) {
    var option = '';
    $.ajax({
        url: url + '/setting/material/specLists',
        type: "POST",
        async: false,
        data: {
            materialId: id,
            token: getToken()
        },
        success: function (r) {
            if (r.code == 100000) {
                for (var i = 0; i < r.data.data.length; i++) {
                    option += `<option value=${r.data.data[i].id} data-unitid="${r.data.data[i].unitId}" data-unitname="${r.data.data[i].unitName}">${r.data.data[i].spec?r.data.data[i].spec:' '}</option>`
                }
            } else {
                tips('规格下拉列表', r);
            }
        }
    });
    return option;
}

/**
 *    给select判断赋值
 */
function selects(select, val) {
    for (var i = 0; i < select.length; i++) {
        if (select.eq(i).val() == val) {
            select.eq(i).prop('selected', 'selected');
        }
    }
}

function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

function getParam(param) {
    var login_msg = JSON.parse(localStorage.getItem('login_msg'));
    return eval('login_msg.' + param);
}

function search() {
    var val = $('input[name=search]').val();
    var this_ind = $('input[name=search]');
    var this_next = $('input[name=search]').next();
    $.ajax({
        type: "post",
        url: url + '/hrm/employee/search',
        beforeSend: function () {
            $('input[name=search]').attr('data-id', '');
            $('input[name=search]').attr('data-number', '');
        },
        async: false,
        data: {
            projectId: getParam('projectId'),
            search: val,
            token: getParam('token')
        },
        success: function (r) {
            this_next.show();
            if (r.data == "" || r.data == null) {
                this_next.html('未查找到此人');
            } else {
                var li = '';
                for (var i = 0; i < r.data.length; i++) {
                    li += `<li data-id=${r.data[i].id} data-number="${r.data[i].jobNumber}">${r.data[i].name}</li>`
                }
                this_next.html(li);
                $('ul li').click(function () {
                    this_ind.val($(this).html());
                    this_ind.data('id', $(this).data('id'));
                    this_ind.data('number', $(this).data('number'));
                    $('ul').hide();
                })
            }
        }
    })
}

function approvalStatus(status) {
    switch (status) {
        case 0:
            return '待审核';
        case 1:
            return '审核中';
        case 2:
            return '已通过';
        case 3:
            return '未通过';
    }
}

/**
 * 价格、纯数字格式整理
 * @param p
 * @param style
 * @returns {string}
 */
function numberFormat(p, style) {
    switch (style) {
        case 'currency':
            option = {style: 'currency', currency: 'CNY'};
            break;
        default:
            option = {minimumFractionDigits: 0, maximumFractionDigits: 4};
    }
    return Number(p).toLocaleString('zh-hans-CN', option)
}

function warehouseSelect(projectId){
    var option = '<option value="">-</option>';
    $.ajax({
        url:url+'/warehouse/warehouse/materialLists',
        type: "POST",
        async: false,
        data: {
            projectId:projectId,
            token:getToken()
        },
        success:function(r){
            if(r.code==100000){
                for(var i = 0; i < r.data.length; i++) {
                    spec = r.data[i].spec?r.data[i].spec:'';
                    option += `
                        <option value=${r.data[i].materialId} data-id="${r.data[i].id}">${r.data[i].materialName}</option>
                    `
                }
            }else{
                tips('仓库库存下拉列表',r);
            }
        }
    });
    return option;
}